<?php
/**
 * Dispatch Return Head
 */
class SDRH extends AppModel {
    var $name = 'SDRH';

    var $useTable = 'supplier_dispatch_return_heads';

    var $hasMany = array(
        'SDR' => array(
            'foreignKey' => 'supplier_dispatch_return_head_id',
            )
    );

    var $belongsTo = array(
            'Supplier',
            'SDH' => array(
                'fields' => array('id', 'dispatch_no'),
                'foreignKey' => 'supplier_dispatch_head_id',
        ),
            'Customer' => array(
                'fields' => array('id', 'cust_no', 'name')
        ),
            'Site' => array(
                'fields' => array('id', 'site_no', 'name')
        ),
            'User' => array(
                'foreignKey' => 'collector_id',
                'fields' => array('username', 'name')
        )
    );

    var $validate = array(
            'return_no' => array(
                    'notEmpty' => array(
                        'rule' => array('notEmpty'),
                        'message' => 'Return no. is required.',
                        'required' => true,
            ),
                    'isUnique' => array(
                        'rule' => 'isUnique',
                        'message' => 'Return no is already taken.',
                        'on' => 'create'
            ),
        ),
            'return_date' => array(
                    'rule' => array('date', 'dmy'),
                    'message' => 'Return date is required.',
                    'required' => true,
        ),
            'supplier_dispatch_head_id' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Dispatch no. is required.',
                    'required' => true,
        ),
            'customer_id' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Customer is required.',
                    'required' => true,
        ),
            'site_id' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Site is required.',
                    'required' => true,
        ),
    );

    function beforeSave() {
        if (isset($this->data['SDRH']['return_date'])) {
            $this->data['SDRH']['return_date'] = $this->dateFormatBeforeSave($this->data['SDRH']['return_date']);
        }
        return true;
    }

    function afterFind($results) {
        foreach ($results as &$custStockOutReturnHead) {
            if (isset($custStockOutReturnHead['SDRH']['return_date'])) {
                $custStockOutReturnHead['SDRH']['return_date'] = $this->dateFormatAfterFind($custStockOutReturnHead['SDRH']['return_date']);
            }
        }
        return $results;
    }
}
?>
